package com.qqtech.qquan.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.hi.service.HiUserService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.user.dao.UserFriendApplyDao;
import com.qqtech.qquan.user.dao.UserFriendDao;
import com.qqtech.qquan.user.enums.UserFriendApplyStatusDictEnum;
import com.qqtech.qquan.user.model.UserFriend;
import com.qqtech.qquan.user.model.UserFriendApply;
import com.qqtech.qquan.user.model.UserFriendApplyQuery;
import com.qqtech.qquan.user.model.UserFriendVo;

@Service
public class UserFriendApplyServiceImpl extends BaseServiceImpl<UserFriendApply>implements UserFriendApplyService {
	public static final Logger log = LoggerFactory.getLogger(UserFriendApplyServiceImpl.class);

	@Resource
	private UserFriendApplyDao userFriendApplyDao;
	@Resource
	private UserFriendDao userFriendDao;
	@Resource
	private PushBizzService pushBizzService;
	@Resource
	private HiUserService hiUserService;


	@Override
	protected BaseDao<UserFriendApply> getBaseDao() {
		return userFriendApplyDao;
	}

	@Override
	public int addFriendApplay(UserFriendApply userFriendApplay) throws BizzException {
		if (userFriendApplay == null) {
			throw new BizzException("圈友申请双方信息不能为空！");
		}
		Integer userId = userFriendApplay.getUserId();
		Integer friendId = userFriendApplay.getFriendId();
		if (userId == null || friendId == null) {
			throw new BizzException("圈友双方id为空！");
		}
		if (userId.intValue() == friendId.intValue()) {
			throw new BizzException("不能加自己为圈友！");
		}
		if (isFriendApplyExist(userId, friendId)) {
			throw new BizzException("您已经提交了圈友申请,不能重复操作！");
		}
		if (userFriendDao.isFriend(userId, friendId)) {
			throw new BizzException("你们已经是圈友关系！");
		}
		userFriendApplay.setStatus(UserFriendApplyStatusDictEnum.WAITING.getCode());
		pushBizzService.applyFriend(friendId);
		return userFriendApplyDao.insert(userFriendApplay);
	}

	@Override
	public boolean isFriendApplyExist(Integer userId, Integer friendId) {
		UserFriendApplyQuery frindApplay = new UserFriendApplyQuery();
		frindApplay.setUserId(userId);
		frindApplay.setFriendId(friendId);
		frindApplay.setStatus(UserFriendApplyStatusDictEnum.WAITING.getCode());
		return userFriendApplyDao.queryCount(frindApplay) > 0;
	}

	@Override
	public int accept(UserFriendApply userFriendApplay) throws BizzException {
		if (userFriendApplay.getStatus() != UserFriendApplyStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个申请！");
		}
		if (userFriendApplay.getUserId() == userFriendApplay.getFriendId()) {
			throw new BizzException("不能加自己为圈友！");
		}
		Integer userId = userFriendApplay.getUserId();
		Integer friendId = userFriendApplay.getFriendId();
		if (userFriendDao.isFriend(userId, friendId)) {
			// throw new BizzException("你们已经是圈友关系！");
			// 如果双方互加后，一方确认，第二方就无法同意，这时删除这条申请
			return userFriendApplyDao.deleteById(userFriendApplay.getId());
		} else {
			int id = 0;
			UserFriendVo friendRecord = userFriendDao.queryFriendRecord(userId, friendId);
			if(friendRecord!=null){
				id = userFriendDao.recoveryFriendShip(userId, friendId);//恢复好友关系
			}else{
				//新增好友关系
				UserFriend friend = new UserFriend();
				friend.setUserId1(userFriendApplay.getUserId());
				friend.setUserId2(userFriendApplay.getFriendId());
				id = userFriendDao.insert(friend);
				if(id>0){
					hiUserService.addFriend(userId, friendId);//荣誉系统--添加好友
				}
			}
			//荣誉系统-ryz
			hiUserService.addFriend(userFriendApplay.getUserId(), userFriendApplay.getFriendId());
			if (id > 0) {
				userFriendApplay.setStatus(UserFriendApplyStatusDictEnum.ACCEPTED.getCode());
				userFriendApplay.setStatusAt(TimeUtil.now());
				userFriendApplyDao.updateById(userFriendApplay);
			}
			return id;
		}

	}

	@Override
	public int ignore(UserFriendApply userFriendApplay) throws BizzException {
		if (userFriendApplay.getStatus() != UserFriendApplyStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个申请！");
		}
		userFriendApplay.setStatus(UserFriendApplyStatusDictEnum.IGNORED.getCode());
		userFriendApplay.setStatusAt(TimeUtil.now());
		return userFriendApplyDao.updateById(userFriendApplay);

	}

	@Override
	public int remove(UserFriendApply userFriendApplay) throws BizzException {
		if (userFriendApplay.getStatus() != UserFriendApplyStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个申请！");
		}
		userFriendApplay.setStatus(UserFriendApplyStatusDictEnum.DELETED.getCode());
		userFriendApplay.setStatusAt(TimeUtil.now());
		return userFriendApplyDao.updateById(userFriendApplay);

	}
	
	@Override
	public int reject(UserFriendApply userFriendApplay) throws BizzException {
		if (userFriendApplay.getStatus() != UserFriendApplyStatusDictEnum.WAITING.getCode()) {
			throw new BizzException("您已经处理过了这个申请！");
		}
		userFriendApplay.setStatus(UserFriendApplyStatusDictEnum.REJECTED.getCode());
		userFriendApplay.setStatusAt(TimeUtil.now());
		return userFriendApplyDao.updateById(userFriendApplay);
	}

}